import { ADD_CART } from "store/contants";

const initCartslist = [];

const cartlist = (state = initCartslist, { type, data }) => {
    switch (type) {
        case ADD_CART:
            const { id } = data;
            let index = state.findIndex((value) => value.id === id);
            if (index !== -1) {
                state[index].num++;
                return [...state];
            }
            return [...state, data];
        default:
            return state;
    }
};

export default cartlist;
